<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/9 0009
 * Time: 下午 5:58
 * Administrator
 */

namespace app\admin\controller;


use app\common\controller\Base;
use app\common\model\Articles as ArticleModel;
use think\db\Query;

class Article extends Base
{
    protected $getList = 'Articles';
    protected $getListWith = 'user';

    public function index()
    {
        return $this->fetch();
    }

    public function article($id)
    {
        $art = ArticleModel::get($id);
        if (!$art){
            return $this->errorInfo('此文章不存在');
        }
        $this->assign('article',$art);
        return $this->fetch();
    }

    public function del($id)
    {
        if (!$this->request->isAjax()){
            return $this->errorInfo('非法访问');
        }
        $art = ArticleModel::get($id);
        if (!$art){
            return $this->errorInfo('此文章不存在');
        }
        $r = $art->delete();
        if ($r){
            return $this->successInfo('成功删除');
        }else{
            return $this->errorInfo('暂时无法删除，请稍后再试。');
        }
    }

    /**
     * 审核文章
     * @param $id
     * @return string
     * @throws \think\exception\DbException
     */
    public function exam($id)
    {
        if (!$this->request->isAjax()){
            return $this->errorInfo('非法访问');
        }
        $art = ArticleModel::get(input('id'));
        $r = $art->save(['status'=>input('status'),'remark'=>input('remark')]);
        if ($r){
            return $this->successInfo('审核成功');
        }else{
            return $this->errorInfo('审核失败，请稍后再试。');
        }
    }
}